package com.wangsanfei.render;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.jfinal.render.Render;
import com.wangsanfei.util.StringUtils;

public class PoiXwpfRender extends Render{

	private XWPFDocument xw;
	private String name;
	
	public PoiXwpfRender(XWPFDocument xw,String name){
		this.xw=xw;
		this.name=name;
	}
	
	public XWPFDocument getXw() {
		return xw;
	}

	public void setXw(XWPFDocument xw) {
		this.xw = xw;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void render() {
		
		String filename="file.docx";
		
		try {
			
			if(!StringUtils.isEmpty(name)){
				filename = URLEncoder.encode(name, "UTF-8");//IE浏览器
			}
			response.addHeader("Content-disposition", "attachment; filename="+filename);
			response.setContentType("application/x-msdownload");
			
			xw.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(xw!=null){
				try {
					xw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

}
